<?php
class Models_Topmenu extends Access_Db {
    function getTopMenu($visible = "visible='1' and ")
    {
        $select = array(
            'id',
            'name',
            $this->lang . '_top_menu_id',
            $this->lang . '_article_id'
        );
        $table = $this->lang . '_top_menu';
        $where = $visible . $this->lang . "_top_menu_id='0' ORDER BY position ASC";
        $this->select($select, $table, $where);
        $result = array();
        while ($row = $this->getSelected()) 
        {
            $result[] = $row;
        }
        return $result;
    }
    
    function setTopMenu($arr)
    {
        $query = "INSERT INTO " . $this->lang . "_articles (title, body, visible, comments, news, user_id, type)
                VALUES (?,?,?,?,?,?,?)";
        $this->prepare($query);
        $this->insert($arr);
        $id = $this->getLAstInsertedID();
        $query = "UPDATE " . $this->lang . "_articles SET created=NOW(), modified=NOW() WHERE id='$id'";
        $this->execQuery($query);
        $select = array('max(position)');
        $from = $this->lang . '_top_menu';
        $this->select($select, $from);
        $maxPosition = $this->getSelected();
        $maxPosition = $maxPosition['max(position)']+1;
        $query = "INSERT INTO " . $this->lang . "_top_menu (name, visible, " . $this->lang . "_article_id, position) VALUES (?,?,?,?)";
        $this->prepare($query);
        $menuArr = array(
            $arr['0'],
            $arr['3'],
            $id,
            $maxPosition
        );
        $this->insert($menuArr);
      $query = "INSERT INTO role_permissions (role_id, article_id, action, controller) VALUES (?,?,?,?)";
      $roleId = $this->getSpecialUserGroup($_COOKIE['user_id']);
      $permissionArr = array( //set permission for edit top menu
          $roleId['role_id'],
          $id,
          'edittop',
          'menu'
      );
      $permissionArr1 = array( //set permissions for delete top menu
          $roleId['role_id'],
          $id,
          'removetop',
          'menu'
      );
      $permissionArr2 = array(
          $roleId['role_id'],
          $id,
          'editingtop',
          'menu'
      );
      $permissionArr3 = array(
          $roleId['role_id'],
          $id,
          'removingtop',
          'menu'
      );
      $permissionArr4 = array(// set permission for add comment to article
          $roleId['role_id'],
          $id,
          'add',
          'comment'
      );
      $permissionArr5 = array(
          $roleId['role_id'],
          '0',
          'adding',
          'comment'
      );
      $this->prepare($query); //init query
      $this->insert($permissionArr);      //insert
      $this->insert($permissionArr1);
      $this->insert($permissionArr2);
      $this->insert($permissionArr3);
      $this->insert($permissionArr4);
      $this->insert($permissionArr5);
    }
    
    function editTopMenu($id, $arr)
    {
        $query = "UPDATE $this->lang" . "_articles SET modified=NOW(), title='" 
                . $arr['0'] . "', body='" 
                . $arr['1'] . "', visible='" 
                . $arr['3'] . "', comments='" 
                . $arr['4'] . "', news='" 
                . $arr['5'] . "' WHERE id='$id'";
        $this->execQuery($query);
        $query = "UPDATE " . $this->lang . "_top_menu SET name='"
              . $arr['0'] . "', visible='"
              . $arr['3'] . "' WHERE " . $this->lang . "_article_id='$id'";
        $this->execQuery($query);
    }
    
    function setPosition($id)
    {
        //TODO
    }
}